// pages/statistics/statistics.js
const { formatDate } = require('../../utils/util.js')

Page({
  data: {
    viewType: 'day',
    currentPeriod: '',
    stats: {
      totalTasks: 0,
      completedTasks: 0,
      completionRate: 0,
      totalTime: 0
    },
    taskDistribution: [],
    efficiency: {
      avgTime: 0,
      bestTime: '',
      completionRate: 0
    }
  },

  onLoad() {
    this.setCurrentPeriod()
    this.loadStatistics()
  },

  setCurrentPeriod() {
    const today = new Date()
    let periodText = ''
    
    switch (this.data.viewType) {
      case 'day':
        periodText = formatDate(today)
        break
      case 'week':
        periodText = this.getWeekRange(today)
        break
      case 'month':
        periodText = today.getFullYear() + '年' + (today.getMonth() + 1) + '月'
        break
      case 'year':
        periodText = today.getFullYear() + '年'
        break
    }
    
    this.setData({
      currentPeriod: periodText
    })
  },

  getWeekRange(date) {
    const day = date.getDay()
    const diff = date.getDate() - day + (day === 0 ? -6 : 1)
    const monday = new Date(date.setDate(diff))
    const sunday = new Date(date.setDate(diff + 6))
    
    return formatDate(monday) + ' - ' + formatDate(sunday)
  },

  loadStatistics() {
    // 模拟统计数据
    const stats = {
      totalTasks: 25,
      completedTasks: 18,
      completionRate: 72,
      totalTime: 450
    }

    const taskDistribution = [
      { levelName: '一级任务', count: 8, percentage: 32 },
      { levelName: '二级任务', count: 12, percentage: 48 },
      { levelName: '三级任务', count: 5, percentage: 20 }
    ]

    const efficiency = {
      avgTime: 25,
      bestTime: '09:00-11:00',
      completionRate: 72
    }

    this.setData({
      stats,
      taskDistribution,
      efficiency
    })

    // 绘制图表
    this.drawChart()
  },

  drawChart() {
    const ctx = wx.createCanvasContext('trendChart', this)
    
    // 模拟图表数据
    const data = [12, 19, 15, 17, 20, 18, 22]
    const maxValue = Math.max(...data)
    const canvasWidth = 600
    const canvasHeight = 200
    const padding = 40
    const chartWidth = canvasWidth - padding * 2
    const chartHeight = canvasHeight - padding * 2
    
    // 绘制坐标轴
    ctx.setStrokeStyle('#E8E8E8')
    ctx.setLineWidth(1)
    
    // X轴
    ctx.moveTo(padding, canvasHeight - padding)
    ctx.lineTo(canvasWidth - padding, canvasHeight - padding)
    
    // Y轴
    ctx.moveTo(padding, padding)
    ctx.lineTo(padding, canvasHeight - padding)
    
    ctx.stroke()
    
    // 绘制数据点
    ctx.setStrokeStyle('#1890FF')
    ctx.setLineWidth(3)
    ctx.setFillStyle('#1890FF')
    
    data.forEach((value, index) => {
      const x = padding + (index / (data.length - 1)) * chartWidth
      const y = canvasHeight - padding - (value / maxValue) * chartHeight
      
      if (index === 0) {
        ctx.moveTo(x, y)
      } else {
        ctx.lineTo(x, y)
      }
      
      // 绘制数据点
      ctx.beginPath()
      ctx.arc(x, y, 4, 0, 2 * Math.PI)
      ctx.fill()
    })
    
    ctx.stroke()
    
    // 绘制标签
    ctx.setFontSize(12)
    ctx.setFillStyle('#666')
    
    // X轴标签
    const days = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    days.forEach((day, index) => {
      const x = padding + (index / (days.length - 1)) * chartWidth
      ctx.fillText(day, x - 15, canvasHeight - padding + 20)
    })
    
    // Y轴标签
    for (let i = 0; i <= 5; i++) {
      const value = Math.round((i / 5) * maxValue)
      const y = canvasHeight - padding - (i / 5) * chartHeight
      ctx.fillText(value.toString(), padding - 25, y + 5)
    }
    
    ctx.draw()
  },

  setViewType(e) {
    const type = e.currentTarget.dataset.type
    this.setData({ viewType: type })
    this.setCurrentPeriod()
    this.loadStatistics()
  },

  onPrevPeriod() {
    // 切换到前一个时间段
    wx.showToast({
      title: '切换到前一个时间段',
      icon: 'none'
    })
  },

  onNextPeriod() {
    // 切换到后一个时间段
    wx.showToast({
      title: '切换到后一个时间段',
      icon: 'none'
    })
  },

  generateReport() {
    wx.navigateTo({
      url: '/pages/report/report?type=daily'
    })
  }
})